﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace WpfAnimation
{
    /// <summary>
    /// PageCustomAnimation.xaml 的交互逻辑
    /// </summary>
    public partial class PageCustomAnimation : Page
    {
        public PageCustomAnimation()
        {
            InitializeComponent();
        }
    }
    public class TestAnimation : DoubleAnimationBase
    {

        // IAnimatable->Animatable->Timeline->AnimationTimeline->DoubleAnimationBase
         //->Storyboard

        public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(double?), typeof(TestAnimation));
        public double? From
        {
            get { return (double?)GetValue(FromProperty); }
            set { SetValue(FromProperty, value); }
        }

        public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(double?), typeof(TestAnimation));
        public double? To
        {
            get { return (double?)GetValue(ToProperty); }
            set { SetValue(ToProperty, value); }
        }

        public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval", typeof(double?), typeof(TestAnimation));
        public double? Interval
        {
            get { return (double?)GetValue(IntervalProperty); }
            set { SetValue(IntervalProperty, value); }
        }

        protected override double GetCurrentValueCore(double defaultOriginValue, double defaultDestinationValue, AnimationClock animationClock)
        {
            double returnValue;
            double from = 0, delta = 0;

            from = From.HasValue ? From.Value : defaultOriginValue;
            delta = To.HasValue ? To.Value - from : defaultDestinationValue - from;

            returnValue = (int)(animationClock.CurrentProgress.Value * delta / 30) * 30 + from;
            if (animationClock.CurrentProgress.Value == 1)
                returnValue = from + delta;

            return returnValue;
        }

        protected override Freezable CreateInstanceCore()
        {
            return new TestAnimation();
        }
    }
}
